import axios from "axios";
export function successResponse(data,message = "请求成功!") {
  return {
    status: "success",
    code: 200,
    message: message,
    data: data,
  };
}

export function errorResponse(message, code = 400) {
  return {
    status: "error",
    code: code,
    message: message,
  };
}

export async function getErpAccessToken() {
  const url = "http://192.168.10.111:17100/api/erp/getRayoErpAccessToken?company_code=AHRY&type=2";
  try {
      const response = await axios.get(url);
      const accessToken = response.data?.data?.access_token;
      //console.log("ERP访问令牌:", accessToken);
      if (!accessToken) {
          throw new Error("ERP访问令牌失败!");
      }
      return accessToken;
  } catch (error) {
      console.error(error.message);
      throw error;
  }
}

export function generateExternalCode() {
  const date = new Date();
  const formattedDate = date.getFullYear().toString() + 
                        String(date.getMonth() + 1).padStart(2, '0') + 
                        String(date.getDate()).padStart(2, '0');
  const randomNumber = String(Math.floor(Math.random() * 99999) + 1).padStart(5, '0');
  return `YM-${formattedDate}${randomNumber}`;
}

export function formatDateTime(dateString) {
  if (!dateString) return null;
  const date = new Date(dateString);
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: false
  }).replace(/\//g, '-');
}